<template>
  <view class="goods-detail-box">
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,index) in goodsInfo.pics" :key="index">
        <image :src="item.pics_big" mode="" class="swiper-item" @click="preview(index)"></image>
      </swiper-item>
    </swiper>
    <view class="goods-info-box" v-if="goodsInfo.goods_name">
      <view class="price">￥{{goodsInfo.goods_price}}</view>
      <view class="goods-info-body">
        <view class="goods-title">{{goodsInfo.goods_name}}</view>
        <view class="star">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="fee">
        快递：免运费 {{cart.length}}
      </view>
    </view>
    <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
    <view class="good-nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick"></uni-goods-nav>
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    data() {
      return {
        goodsId: '',
        goodsInfo: {},
        options: [{
          icon: 'shop',
          text: '店铺',
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    onLoad(option) {
      this.goodsId = option.goods_id
      this.getCateList()
    },
    computed: {
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart', ['total'])
    },
    watch: {
      total: {
        handler(value) {
          const cart = this.options.find(item => item.text === '购物车')
          cart.info = value
        },
        immediate: true
      }
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id: this.goodsId
        })
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.goodsInfo = res.message
        this.goodsInfo.goods_introduce = this.goodsInfo.goods_introduce.replace(/<img /g,
          '<img style="display:block;"').replace(/\.webp?/ig, ".jpg")
      },
      preview(index) {
        uni.previewImage({
          current: index,
          urls: this.goodsInfo.pics.map(item => item.pics_big),
        })
      },
      onClick(e) {
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      buttonClick(e) {
        if (e.content.text === '加入购物车') {
          const goods = {
            goods_id: this.goodsId, // 商品的Id
            goods_name: this.goodsInfo.goods_name, // 商品的名称
            goods_price: this.goodsInfo.goods_price, // 商品的价格
            goods_count: 1, // 商品的数量
            goods_small_logo: this.goodsInfo.goods_small_logo, // 商品的图片
            goods_state: true // 商品的勾选状态
          }
          this.addToCart(goods)
        }

      }
    }
  }
</script>

<style lang="scss">
  .goods-detail-box {
    padding-bottom: 50px;

    swiper {
      height: 750rpx;

      .swiper-item {
        width: 100%;
        height: 100%;
      }
    }

    .goods-info-box {
      padding: 10px 0 10px 10px;

      .price {
        color: #c00000;
        font-size: 18px;
        margin: 10px 0;
      }

      .goods-info-body {
        display: flex;
        justify-content: space-between;

        .goods-title {
          font-size: 13px;
        }

        .star {
          width: 120px;
          font-size: 12px;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          border-left: 1px solid #efefef;
          color: gray;
        }

      }

      .fee {
        margin: 10px 0;
        color: gray;
        font-size: 12px;
      }
    }

    .good-nav {
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
    }
  }
</style>